<template>
  <div>
    <Header />

    <!-- 商品头部信息 -->
    <section class="product-header">
      <div class="container">
        <div class="detail">
          <!-- 走马灯组件 -->
          <div class="carousel-container">
            <div class="carousel">
              <div class="carousel-inner" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
                <div class="carousel-item" v-for="(image, index) in productImages" :key="index">
                  <img
                      :src="image"
                      alt="Product Image"
                      class="carousel-image"
                      @click="openImagePreview(index)"
                  >
                </div>
              </div>

              <!-- 控制按钮 -->
              <button class="carousel-control prev" @click="prevSlide">
                <i class="fa fa-angle-left"></i>
              </button>
              <button class="carousel-control next" @click="nextSlide">
                <i class="fa fa-angle-right"></i>
              </button>
            </div>
          </div>

          <div class="info">
            <div class="product-info">
              <h1 class="product-title">{{ product.name }}</h1>
              <p class="product-desc">{{ product.descriptions }}</p>
              <!-- 商品名称区域 -->
              <div class="price-container">
                <div class="product-name">商品名称</div>
                <div class="product-value">{{ product.name }}</div>
              </div>
              <!-- 价格区域 -->
              <div class="price-container price-stock">
                <div class="price-label">价格</div>
                <div class="price-value">￥{{ product.price }}</div>
              </div>
              <div class="price-container">
                <div class="price-label">库存</div>
                <div class="price-value">{{ product.numbers }}</div>
              </div>
              <div class="price-container">
                <div class="price-label">创建时间</div>
                <div class="price-value">{{ product.createTime }}</div>
              </div>
              <div class="price-container">
                <div class="price-label">详细描述</div>
                <div class="price-value">{{ product.descriptionsInfo }}</div>
              </div>
              <div class="price-container">
                <div class="price-label">商品介绍</div>
                <div class="price-value">{{ product.introduce }}</div>
              </div>

              <!-- 优惠区域 -->
              <div class="promotion-container">
                <div class="promotion-label">优惠</div>
                <div class="promotion-content">
                  <span class="tag">满减</span>
                  <span class="text">满100减20</span>
                </div>
              </div>

              <!-- 颜色选择 -->
              <div class="color-container">
                <div class="color-label">颜色</div>
                <div class="color-options">

                  <!-- 固定颜色选择按钮 -->
                  <div class="fixed-color-buttons">
                    <button
                        class="color-btn"
                        :class="{ 'active': selectedColor === 'red' }"
                        @click="selectedColor = 'red'"
                        style="background-color: #f6f1f1;">
                      <span class="color-text">红色</span>
                    </button>
                    <button
                        class="color-btn"
                        :class="{ 'active': selectedColor === 'blue' }"
                        @click="selectedColor = 'blue'"
                        style="background-color: #f6f1f1;">
                      <span class="color-text">蓝色</span>
                    </button>
                  </div>
                </div>
              </div>

              <!-- 数量选择 -->
              <div class="quantity-container">
                <div class="quantity-label">数量</div>
                <div class="quantity-control">
                  <el-input-number v-model="quantity" :min="1" :max="999"></el-input-number>
                </div>
                <!-- 新增数量加减组件 -->
                <div class="quantity-buttons">
                  <button class="quantity-btn minus" @click="decreaseQuantity">-</button>
                  <span class="quantity-display">{{ quantity }}</span>
                  <button class="quantity-btn plus" @click="increaseQuantity">+</button>
                </div>
              </div>
            </div>

            <!-- 按钮容器 -->
            <div class="btn-container">
              <button class="buy-btn" @click="buyNow">立即购买</button>
              <button class="cart-btn" @click="addToCart">加入购物车</button>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 商品内容区域 -->
    <section class="product-content">
      <div class="container">
        <!-- 商品介绍 -->
        <div class="tab-content">
          <h2 class="tab-title">商品介绍</h2>
          <div class="product-detail" v-html="product.introduce || '暂无介绍内容'"></div>
        </div>
      </div>
    </section>

    <!-- 底部服务承诺 -->
    <div class="service-promises">
      <div class="service-item">
        <div class="service-icon">多</div>
        <div class="service-text">品类齐全，轻松购物</div>
      </div>
      <div class="service-item">
        <div class="service-icon">快</div>
        <div class="service-text">多仓直发，极速配送</div>
      </div>
      <div class="service-item">
        <div class="service-icon">好</div>
        <div class="service-text">正品行货，精致服务</div>
      </div>
      <div class="service-item">
        <div class="service-icon">省</div>
        <div class="service-text">天天低价，畅选无忧</div>
      </div>
    </div>
    <!-- 联系客服按钮 -->
    <div class="contact-customer-service">
      <button class="contact-btn" @click="toggleServiceCard">
        <i class="fa fa-comments"></i>
        <span>联系客服</span>
      </button>

      <!-- 客服悬浮卡片 -->
      <div class="service-card" :style="{ display: showContactCard ? 'block' : 'none' }">
        <div class="card-header">
          <h3>在线客服</h3>
          <button class="close-btn" @click="closeServiceCard">&times;</button>
        </div>
        <div class="card-content">
          <p>工作时间: 9:00-18:00，不在工作时间内可以发送邮件到1871794553@qq.com哦，亲~~</p>
          <div class="contact-methods">
            <a href="tel:19511988246" class="contact-item">
              <i class="fa fa-phone"></i>
              <span>电话咨询</span>
            </a>
            <a href="https://mail.qq.com/" class="contact-item">
              <i class="fa fa-envelope"></i>
              <span>邮件咨询</span>
            </a>
            <a href="#" class="contact-item" @click="openChatWindow">
              <i class="fa fa-comments"></i>
              <span>在线聊天</span>
            </a>
          </div>
        </div>
      </div>
    </div>

    <Footer />

  </div>
  <!-- 聊天窗口 -->
  <ChatWindow v-model:show="showChatWindow" />

  <!-- 图片预览模态框 -->
  <div
      class="image-preview-modal"
      :style="{ display: showImagePreview ? 'flex' : 'none' }"
      @click="closeImagePreview"
  >
    <div class="image-preview-content" @click.stop>
      <button class="close-preview" @click="closeImagePreview">
        <i class="fa fa-times"></i>
      </button>
      <img
          :src="productImages[currentPreviewIndex]"
          alt="Enlarged Product Image"
          class="preview-image"
      >
      <button class="preview-control prev" @click="prevPreviewImage">
        <i class="fa fa-angle-left"></i>
      </button>
      <button class="preview-control next" @click="nextPreviewImage">
        <i class="fa fa-angle-right"></i>
      </button>
    </div>
  </div>

  <!-- 购买弹窗 -->
  <div v-if="showBuyModal" class="buy-modal-mask">
    <div class="buy-modal">
      <div class="buy-modal-title">确认下单</div>
      <div class="buy-modal-row">价格：<span style="color:#e00b0b;font-weight:bold">￥{{ product.price }}</span></div>
      <div class="buy-modal-row">优惠后金额：<span style="color:#e00b0b;font-weight:bold">￥{{ finalPrice }}</span></div>
      <div class="buy-modal-row">
        <label><input type="checkbox" v-model="useCoupon" />使用优惠券</label>
        <select v-model="selectedCouponId" :disabled="!useCoupon" style="margin-left:10px">
          <option value="">请选择优惠券</option>
          <option v-for="c in couponList" :key="c.bagId" :value="c.bagId">{{ c.name }}（满{{c.userLimitValue}}减{{c.value}}）</option>
        </select>
      </div>
      <div class="buy-modal-row">
        <div style="margin-bottom:6px;">选择收货地址：</div>
        <ul v-if="addressList.length" class="pay-address-list">
          <li v-for="address in addressList" :key="address.id" class="pay-address-item" :class="{selected: buyAddressId === address.id}" @click="buyAddressId = address.id">
            <div>
              <b>{{ address.name }}</b> {{ address.phone }}<span v-if="address.isDefault" style="color:#2563eb;font-size:12px;margin-left:8px;">[默认]</span><br>
              <span style="font-size:13px;color:#888;">{{ address.area }} {{ address.otherName }}</span>
            </div>
          </li>
        </ul>
        <div v-else style="color:#888;">暂无收货地址，请先在用户中心添加</div>
      </div>
      <div class="buy-modal-row" style="text-align:right">
        <button @click="closeBuyModal" :disabled="buyLoading">取消</button>
        <button @click="confirmBuy" :disabled="buyLoading || !buyAddressId" style="margin-left:10px;color:#fff;background:#e00b0b;padding:4px 16px;border:none;border-radius:4px">确认下单</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'
import { ref, onMounted, onUnmounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getProductDetail } from '@/api/product'
import { useCartStore } from '@/store/cart'
import ChatWindow from "@/components/ChatWindow.vue";
import { addToCart as apiAddToCart } from '@/api/cart'
import { payOrder, createOrder } from '@/api/order'
import { getQuan } from '@/api/user'
import { getAddressPage } from '@/api/address'

const showChatWindow = ref(false);
const showImagePreview = ref(false);
const currentPreviewIndex = ref(0);
const showBuyModal = ref(false)
const couponList = ref<any[]>([])
const selectedCouponId = ref('')
const useCoupon = ref(true)
const buyLoading = ref(false)

const route = useRoute()
const router = useRouter()
const product = ref({
  id: '',
  name: '',
  descriptions: '',
  price: 0,
  img: '',
  numbers: 0,
  descriptionsInfo: '',
  introduce: '',
  secondaryCategoryId: '',
  createTime: '',
})
const currentIndex = ref(0)
const productImages = ref<string[]>([])
let carouselInterval: ReturnType<typeof setInterval> | null = null

// 商品属性选择
const selectedColor = ref('')
const quantity = ref(1)

const finalPrice = computed(() => {
  if (!useCoupon.value) return product.value.price
  const coupon = couponList.value.find(c => c.bagId == selectedCouponId.value)
  if (coupon && product.value.price >= coupon.userLimitValue) {
    return Math.max(0, product.value.price - coupon.value)
  }
  return product.value.price
})

const addressList = ref<any[]>([])
const buyAddressId = ref<number|null>(null)

onMounted(async () => {
  try {
    const res = await getProductDetail(route.params.id as string)
    if(res && res.code === 200 && res.data) {
      product.value = res.data
      // 只用主图
      productImages.value = res.data.img ? [res.data.img] : []
    }
  } catch (error) {
    console.error('获取商品详情失败:', error)
  }

  // 自动轮播（如果有多图可扩展）
  if(productImages.value.length > 1) {
    carouselInterval = setInterval(() => {
      currentIndex.value = (currentIndex.value + 1) % productImages.value.length
    }, 3000)
  }
})

onUnmounted(() => {
  if (carouselInterval) {
    clearInterval(carouselInterval)
  }
})

const cartStore = useCartStore()
async function addToCart() {
  try {
    await apiAddToCart(product.value.id, quantity.value)
    window.alert('加入购物车成功！')
  } catch (e) {
    window.alert('加入购物车失败！')
  }
}
const openChatWindow = () => {
  showContactCard.value = false;
  showChatWindow.value = true;
};

async function openBuyModal() {
  showBuyModal.value = true
  // 获取优惠券
  try {
    const res = await getQuan({ pageNum: 1, pageSize: 10 })
    if(res && res.quanVOs) {
      couponList.value = res.quanVOs
    } else {
      couponList.value = []
    }
  } catch {
    couponList.value = []
  }
  // 获取真实地址
  try {
    const res = await getAddressPage({ page: 1, size: 10 })
    if(res && res.data && Array.isArray(res.data.records)) {
      addressList.value = res.data.records
      buyAddressId.value = addressList.value.find(a => a.isDefault)?.id || (addressList.value[0]?.id || null)
    } else {
      addressList.value = []
      buyAddressId.value = null
    }
  } catch {
    addressList.value = []
    buyAddressId.value = null
  }
}

function closeBuyModal() {
  showBuyModal.value = false
}

async function confirmBuy() {
  buyLoading.value = true
  try {
    // 参数校验
    const pid = Number(product.value.id)
    const qty = Number(quantity.value)
    const addrId = Number(buyAddressId.value)
    let quanId = 0
    if (useCoupon.value && selectedCouponId.value) {
      quanId = Number(selectedCouponId.value)
    }
    if (!pid || !qty || !addrId) {
      window.alert('请选择商品、数量和收货地址！')
      buyLoading.value = false
      return
    }
    // 先创建订单，参数严格按后端要求
    const res = await createOrder({
      productIds: [pid],
      numbers: [qty],
      addressId: addrId,
      quanId: quanId
    })
    if(res && res.code === 200 && res.data && res.data.orderId) {
      window.alert('下单成功！请前往个人中心订单页支付')
      showBuyModal.value = false
      router.push('/user?tab=orders')
    } else {
      window.alert(res.message || '下单失败！')
    }
  } catch (e) {
    window.alert('下单失败！')
  }
  buyLoading.value = false
}

// 修改buyNow按钮事件
function buyNow() {
  openBuyModal()
}

// 添加轮播控制方法
function prevSlide() {
  currentIndex.value = (currentIndex.value - 1 + productImages.value.length) % productImages.value.length
}

function nextSlide() {
  currentIndex.value = (currentIndex.value + 1) % productImages.value.length
}

// 添加数量控制方法
function decreaseQuantity() {
  if (quantity.value > 1) {
    quantity.value--;
  }
}

function increaseQuantity() {
  if (quantity.value < 999) {
    quantity.value++;
  }
}

// 控制客服卡片显示/隐藏
const showContactCard = ref(false)

// 显示/隐藏客服卡片
const toggleServiceCard = () => {
  showContactCard.value = !showContactCard.value
}

// 关闭客服卡片
const closeServiceCard = () => {
  showContactCard.value = false
}

// 图片预览功能
function openImagePreview(index: number) {
  currentPreviewIndex.value = index;
  showImagePreview.value = true;
  document.body.style.overflow = 'hidden'; // 防止背景滚动
}

function closeImagePreview() {
  showImagePreview.value = false;
  document.body.style.overflow = ''; // 恢复背景滚动
}

function prevPreviewImage() {
  currentPreviewIndex.value = (currentPreviewIndex.value - 1 + productImages.value.length) % productImages.value.length;
}

function nextPreviewImage() {
  currentPreviewIndex.value = (currentPreviewIndex.value + 1) % productImages.value.length;
}
</script>

<style scoped>
.header{
  width:100vw;
}
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

.product-header {
  padding: 24px 280px;
}

.detail {
  display: flex;
}

/* 走马灯 */
.carousel-container {
  width: 400px;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}

.carousel {
  position: relative;
  height: 400px;
}

.carousel-inner {
  display: flex;
  transition: transform 0.5s ease;
  height: 100%;
}

.carousel-item {
  flex: 0 0 100%;
  height: 100%;
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  cursor: zoom-in;
}

.carousel-indicators button {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  border: none;
  cursor: pointer;
}

.carousel-indicators button.active {
  background-color: white;
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.3);
  color: white;
  border: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.3s;
}

.carousel-control:hover {
  background-color: rgba(0, 0, 0, 0.6);
}

.prev {
  left: 10px;
}

.next {
  right: 10px;
}

.info {
  flex: 1; /* 信息区域占据剩余空间 */
  margin-left: 24px; /* 添加适当间距 */
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

/* 商品信息样式 */
.product-info {
  text-align: left;
  width: 400px;
  font-size: 15px;
}
.product-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 16px;
}
.product-desc {
  color: #807b7b;
  margin-bottom: 16px;
  font-size: 14px;
}
.price-container {
  display: flex;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}
.price-label {
  width: 80px;
  color: #807b7b;
  font-size: 14px;
}
.price-value {
  font-size: 16px;
  color: #e00b0b;
  font-weight: bold;
}
.product-value {
  color: #222;
  font-size: 15px;
  font-weight: normal;
}
/* 只让价格和库存为红色 */
.price-container.price-stock .price-value {
  color: #e00b0b;
  font-weight: bold;
}
.price-container:not(.price-stock) .price-value {
  color: #222;
  font-weight: normal;
}

.promotion-container,
.color-container,
.quantity-container {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #eee;
}

.promotion-label,
.color-label,
.quantity-label {
  width: 80px;
  color: #807b7b;
  font-size: 14px;
}

.promotion-content .tag {
  background-color: #e00b0b;
  color: white;
  padding: 2px 4px;
  font-size: 12px;
  margin-right: 8px;
}
.promotion-content .text {
  background-color: #e00b0b;
  color: white;
  padding: 2px 4px;
  font-size: 12px;
  margin-right: 8px;
}

/* 按钮容器 */
.btn-container {
  display: flex;
  gap: 16px;
  margin-top: 24px;
}

/* 按钮样式 */
.buy-btn, .cart-btn {
  padding: 12px 32px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  font-weight: bold;
  min-width: 160px;
}

.buy-btn {
  background-color: #ff4444;
  color: white;
  border: 1px solid #ff4444;
}

.buy-btn:hover {
  background-color: #e63939;
}

.cart-btn {
  background-color: #ffb800;
  color: white;
  border: 1px solid #ffb800;
}

.cart-btn:hover {
  background-color: #e6a600;
}

/* 内容区域 */
.tab-content {
  padding: 32px 0;
}

.tab-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid #eee;
}

.product-detail img {
  max-width: 100%;
  height: auto;
  margin-bottom: 16px;
}

/* 新增的固定颜色按钮样式 */
.fixed-color-buttons {
  display: flex;
  gap: 8px;
  margin-left: 10px;
  transform: translateX(-20px);
}

.color-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 60px;
  height: 32px;
  border-radius: 16px;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px #ddd;
  cursor: pointer;
  transition: all 0.2s ease;
  color: white;
  font-size: 14px;
  padding: 0 8px;
}

.color-btn.active {
  box-shadow: 0 0 0 2px #ff6700;
  transform: scale(1.05);
}

.color-text {
  color: black;
  text-shadow: none;
}

/* 新增数量按钮样式 */
.quantity-buttons {
  display: flex;
  align-items: center;
  margin-left: 16px;
  transform: translateX(-20px);
}

.quantity-btn {
  width: 32px;
  height: 32px;
  border: 1px solid #ddd;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.2s;
}

.quantity-btn:hover {
  background-color: #e8e8e8;
}

.quantity-display {
  width: 40px;
  height: 32px;
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.minus {
  border-radius: 4px 0 0 4px;
}

.plus {
  border-radius: 0 4px 4px 0;
}

/* 底部服务承诺样式 */
.service-promises {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 24px 0;
  background-color: #f8f8f8;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
}
.chat-modal {
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1002;
}

.service-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 25%;
  padding: 16px 0;
  transform: translateX(50px);
}

.service-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background-color: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
}

.service-text {
  font-size: 14px;
  color: #666;
  text-align: center;
}

/* 联系客服按钮样式 */
.contact-customer-service {
  position: fixed;
  bottom: 30px;
  right: 30px;
  z-index: 1000;
}

.contact-btn {
  background-color: #ff4444;
  color: white;
  border: none;
  border-radius: 50px;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
}

.contact-btn:hover {
  background-color: #e63939;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

.contact-btn i {
  font-size: 18px;
}

/* 客服卡片样式 */
.service-card {
  position: absolute;
  bottom: 60px;
  right: 0;
  width: 280px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
  display: none;
  transform-origin: bottom right;
  animation: fadeIn 0.3s ease forwards;
  z-index: 1001;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.card-header {
  padding: 12px 16px;
  background-color: #f8f8f8;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-radius: 8px 8px 0 0;
}

.card-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 18px;
  color: #888;
  cursor: pointer;
  transition: color 0.2s;
}

.close-btn:hover {
  color: #333;
}

.card-content {
  padding: 16px;
}

.card-content p {
  margin: 0 0 12px;
  color: #666;
  font-size: 14px;
}

.contact-methods {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 4px;
  background-color: #f5f5f5;
  color: #333;
  text-decoration: none;
  transition: background-color 0.2s;
}

.contact-item:hover {
  background-color: #e8e8e8;
}

.contact-item i {
  width: 20px;
  text-align: center;
  color: #ff4444;
}

/* 图片预览模态框样式 */
.image-preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.8);
  display: none;
  justify-content: center;
  align-items: center;
  z-index: 2000;
  cursor: zoom-out;
}

.image-preview-content {
  position: relative;
  max-width: 90%;
  max-height: 90%;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}

.preview-image {
  max-width: 100%;
  max-height: 85vh;
  display: block;
  margin: 0 auto;
}

.close-preview {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
}

.preview-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
}

.preview-control.prev {
  left: 10px;
}

.preview-control.next {
  right: 10px;
}

.buy-modal-mask {
  position: fixed;
  left: 0;top: 0;right: 0;bottom: 0;
  background: rgba(0,0,0,0.3);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.buy-modal {
  background: #fff;
  border-radius: 8px;
  min-width: 320px;
  padding: 24px 32px 16px 32px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
}
.buy-modal-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 18px;
}
.buy-modal-row {
  margin-bottom: 16px;
  font-size: 15px;
}

.pay-address-list {
  padding: 0;
  margin: 0;
  list-style: none;
}
.pay-address-item {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 12px 16px;
  margin-bottom: 10px;
  background: #f8fafc;
  cursor: pointer;
  transition: border 0.2s, box-shadow 0.2s;
}
.pay-address-item.selected {
  border: 2px solid #2563eb;
  background: #e0e7ff;
  box-shadow: 0 2px 8px #e0e7ff44;
}

@media (max-width: 900px) {
  .product-header {
    padding: 16px 4vw;
  }
  .detail {
    flex-direction: column;
    align-items: center;
  }
  .carousel-container {
    width: 90vw;
    max-width: 400px;
    margin: 0 auto 18px auto;
  }
  .info {
    margin-left: 0;
    width: 100%;
    max-width: 480px;
  }
  .product-info {
    width: 100%;
    font-size: 14px;
  }
}
@media (max-width: 600px) {
  .product-header {
    padding: 8px 0;
  }
  .carousel-container {
    width: 100vw;
    max-width: 100vw;
    height: 220px;
  }
  .carousel {
    height: 220px;
  }
  .info {
    max-width: 100vw;
    padding: 0 8px;
  }
  .product-info {
    width: 100%;
    font-size: 13px;
  }
  .btn-container {
    flex-direction: column;
    gap: 10px;
    margin-top: 16px;
  }
  .buy-btn, .cart-btn {
    min-width: 0;
    width: 100%;
    font-size: 15px;
    padding: 10px 0;
  }
  .service-promises {
    flex-direction: column;
    gap: 8px;
    padding: 12px 0;
  }
  .service-item {
    width: 100%;
    padding: 8px 0;
  }
  .contact-customer-service {
    right: 10px;
    bottom: 10px;
  }
}
</style>
